import { compressImage } from '@/utils/tools';
import dayjs  from 'dayjs';


export function showToast({
  title = '',
  icon = 'none',
  mask = false,
  duration = 1500,
} = {}) {
  uni.showToast({
    title,
    icon,
    mask,
    duration,
  });
}

//显示模态弹窗
export function showModal({ content = '', showCancel = true } = {}) {
  return new Promise((resolve, reject) => {
    uni.showModal({
      title: '提示',
      content,
      showCancel,
      success: ({ confirm }) => {
        if (confirm) resolve('confirm');
        else resolve('cancel');
      },
      fail: () => {
        reject('fail');
      },
    });
  });
}

//路由跳转方法
export function routerTo(url, type = 'navigateTo') {
  if (type == 'navigateTo') {
    uni.navigateTo({
      url,
    });
  } else if (type == 'redirectTo') {
    uni.redirectTo({
      url,
    });
  } else if (type == 'switchTab') {
    uni.switchTab({
      url,
    });
  } else if (type == 'reLaunch') {
    uni.reLaunch({
      url,
    });
  } else {
    return '错误信息';
  }
}

export const previewImage = (url) => {
  uni.previewImage({
    urls: [url],
  });
};

export const uploadFile = async (tempurl) => {
  const blob = await compressImage(	tempurl);
  return uniCloud.uploadFile({
    filePath: blob,
    cloudPath:
      'wallpaper/' + dayjs().format('YYYYMMDD') + '/' + Date.now() + '.webp',
  });
};
